package cj.primariaiquest.ws.endpoint;

import org.springframework.oxm.Marshaller;
import org.springframework.oxm.Unmarshaller;
import org.springframework.ws.client.core.WebServiceTemplate;
import org.springframework.ws.soap.client.SoapFaultClientException;

import cj.primariaiquest.schemas.CancelTicketRequest;
import cj.primariaiquest.schemas.ObjectFactory;
import cj.primariaiquest.schemas.TicketRequest;
import cj.primariaiquest.schemas.TicketResponse;
import cj.primariaiquest.ws.exceptions.WebServiceException;

/**
 * Classed used to simulate a web service client. This way we can send requests
 * directly to the web service.
 * 
 * @author georgian.parvu
 * 
 */
public class WebServiceClient {

    /**
     * Field used to send messages to web service
     */
    private final WebServiceTemplate webServiceTemplate = new WebServiceTemplate();

    /**
     * Object factory used to create request and response objects for web
     * service
     */
    private final ObjectFactory objFactory = new ObjectFactory();

    /**
     * Set the default URI for web service calls
     * 
     * @param defaultUri
     */
    public void setDefaultUri(String defaultUri) {
	webServiceTemplate.setDefaultUri(defaultUri);
    }

    public void setMarshaller(Marshaller marshaller) {
	webServiceTemplate.setMarshaller(marshaller);
    }

    public void setUnmarshaller(Unmarshaller unmarshaller) {
	webServiceTemplate.setUnmarshaller(unmarshaller);
    }

    /**
     * Sends a ticket request for the given department. Returns a ticket
     * response with the next number.
     * 
     * @param department
     *            for request message
     * @return TicketResponse
     * @throws WebServiceException
     *             if no more numbers are available for the required department
     */
    public TicketResponse sendTicketRequestAndReceive(final String department)
	    throws WebServiceException {

	// create a request object and set the department for it
	TicketRequest request = objFactory.createTicketRequest();
	request.setDepartment(department);

	// use marshalSendAndReceive method to return an unmarshalled object
	TicketResponse response;
	try {
	    response = (TicketResponse) webServiceTemplate
		    .marshalSendAndReceive(request);
	} catch (SoapFaultClientException e) {
	    throw new WebServiceException(e.getMessage());
	}

	return response;
    }

    /**
     * Sends a cancel ticket request for the given department and ticket number.
     * 
     * @param department
     *            for request message
     * @return TicketResponse
     * @throws WebServiceException
     *             if introduced data is invalid
     */
    public void sendCancelTicketRequest(final String department,
	    final Long number) throws WebServiceException {

	// create a request object and set the department for it
	CancelTicketRequest request = objFactory.createCancelTicketRequest();
	request.setDepartment(department);
	request.setNumber(number);

	// use marshalSendAndReceive method to return an unmarshalled object
	try {
	    webServiceTemplate.marshalSendAndReceive(request);
	} catch (SoapFaultClientException e) {
	    throw new WebServiceException(e.getMessage());
	}

    }
}